package org.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.reggie.dto.SetmealDto;
import org.example.reggie.entry.Category;
import org.example.reggie.entry.Setmeal;
import org.example.reggie.result.ResultData;
import org.example.reggie.service.CategoryService;
import org.example.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐 controller控制类
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 保存套餐
     * @return
     */
    @PostMapping("/insert")
    public ResultData<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return ResultData.success("保存成功");
    }

    /**
     * 分页查询，这种查询方式简直鸡肋，最好用mybatis分页插件，在编写sql语句
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public ResultData<Page> page(Integer pageNum,Integer pageSize,String name){
        Page<Setmeal> pageInfo = new Page<>(pageNum,pageSize);
        Page<SetmealDto> dtopage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(null!=name,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //查询到的数据会自动填充到pageInfo
        setmealService.page(pageInfo,queryWrapper);
        //拷贝对象 SetmealDto才是展示到页面的实体
        // records 因为泛型，所以忽略掉这个
        BeanUtils.copyProperties(pageInfo,dtopage,"records");
        List<Setmeal> reodrs = pageInfo.getRecords();
        List<SetmealDto> list = reodrs.stream().map((item)->{
            SetmealDto dto = new SetmealDto();
            BeanUtils.copyProperties(item,dto);
            Long id= item.getCategoryId();
            Category category = categoryService.getById(id);
            if(null != category){
                dto.setCategoryName(category.getName());
            }
            return dto;
        }).collect(Collectors.toList());
        dtopage.setRecords(list);
        return ResultData.success("查询成功",dtopage);
    }

    /**
     *  setmealCache ,使用Cacheable缓存起来
     * @param setmeal
     * @return
     */
    @Cacheable(value="setmealCache",key="#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public ResultData<List> getList(@RequestBody Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> query=new LambdaQueryWrapper<>();
        query.eq(null != setmeal.getCategoryId(),Setmeal::getCategoryId,setmeal.getCategoryId());
        query.eq(null != setmeal.getStatus(),Setmeal::getStatus,setmeal.getStatus());
        query.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(query);
        return ResultData.success(list);
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @CacheEvict(value="setmealCache",allEntries = true)
    @GetMapping("/deleteIds")
    public ResultData<String> deleteIds(@PathVariable  List<Long> ids){
        setmealService.removeWithDish(ids);
        return ResultData.success("删除成功");

    }
}
